package com.qef.com.mmovie.mvp.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qef.com.mmovie.App;
import com.qef.com.mmovie.R;
import com.qef.com.mmovie.base.BaseRecycerViewAdapter;
import com.qef.com.mmovie.entity.DataBean;
import com.qef.com.mmovie.entity.SubscriptionDetailsEntity;
import com.qef.com.mmovie.img.ImageLoader;
import com.qef.com.mmovie.mvp.activity.VideoDetailActivity;
import com.qef.com.mmovie.utils.DateUtil;
import com.qef.com.mmovie.utils.UIHelper;

import java.util.List;

import static android.R.attr.data;

/**
 * Created by guangjiego on 2017/7/10.
 */

public class itemDetailsInfoAdapter extends BaseRecycerViewAdapter<SubscriptionDetailsEntity.ItemListEntityX.DataEntityX.ItemListEntity, itemDetailsInfoAdapter.MyHolder> {


    public itemDetailsInfoAdapter(Context context, List<SubscriptionDetailsEntity.ItemListEntityX.DataEntityX.ItemListEntity> list) {
        super(context, list);
    }

    @Override
    public MyHolder getCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(View.inflate(App.getInstance(), R.layout.item_details_info, null));
    }

    @Override
    public void getBindViewHolder(MyHolder holder, int position) {
        ImageLoader.getInstance().displayImage(App.getInstance(), list.get(position).getData().getCover().getFeed(), holder.ivBg);
        holder.tvDis.setText(list.get(position).getData().getTitle());
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("#").append(list.get(position).getData().getCategory())
                .append(" ")
                .append(" / ")
                .append(" ")
                .append(DateUtil.formatTime2(list.get(position).getData().getDuration()));
        holder.tvDisx.setText(stringBuilder.toString());
        holder.itemView.setOnClickListener(v -> {
            SubscriptionDetailsEntity.ItemListEntityX.DataEntityX.ItemListEntity.DataEntity data = list.get(position).getData();
            DataBean dataBean = getDataBean(data);
            context.startActivity(new Intent(context, VideoDetailActivity.class).
                    putExtra(VideoDetailActivity.VIDEO_INFOS, dataBean)
            );

        });
    }

    @NonNull
    private DataBean getDataBean(SubscriptionDetailsEntity.ItemListEntityX.DataEntityX.ItemListEntity.DataEntity data) {
        DataBean dataBean = new DataBean();
        dataBean.setDataType(data.getDataType());
        dataBean.setId(data.getId());
        dataBean.setTitle(data.getTitle());
        dataBean.setDescription(data.getDescription());
        dataBean.setProvider(data.getProvider());
        dataBean.setCategory(data.getCategory());
        dataBean.setCover(data.getCover());
        dataBean.setPlayUrl(data.getPlayUrl());
        dataBean.setDuration(data.getDuration());
        dataBean.setWebUrl(data.getWebUrl());
        dataBean.setReleaseTime(data.getReleaseTime());
        dataBean.setConsumption(data.getConsumption());
        dataBean.setType(data.getType());
        dataBean.setIdx(data.getIdx());
        dataBean.setDate(data.getDate());
        dataBean.setCollected(false);
        dataBean.setPlayed(false);
        dataBean.setPlayInfo(data.getPlayInfo());
        dataBean.setTags(data.getTags());
        return dataBean;
    }

    class MyHolder extends RecyclerView.ViewHolder {
        ImageView ivBg;
        TextView tvDis;
        TextView tvDisx;

        public MyHolder(View itemView) {
            super(itemView);
            ivBg = (ImageView) itemView.findViewById(R.id.iv_bg);
            tvDis = (TextView) itemView.findViewById(R.id.tv_dis);
            tvDisx = (TextView) itemView.findViewById(R.id.tv_disx);
        }
    }
}
